<template>
  <div class="article-detail-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-content">
        <div class="logo-section">
          <img src="https://py-steven.com/blog/static/image/logo.png" alt="logo" class="logo" />
          <span class="site-title">{{ article?.title || '文章详情' }}</span>
          <span class="site-subtitle">blog</span>
        </div>
        <nav class="nav-menu">
          <router-link to="/home" class="nav-item active">首页</router-link>
          <a href="#" class="nav-item">笔记</a>
          <div class="nav-item dropdown">
            功能 <span class="arrow">▼</span>
            <div class="dropdown-menu">
              <a href="#" class="dropdown-item">大赛练习</a>
              <a href="#" class="dropdown-item">错题下载</a>
            </div>
          </div>
          <router-link to="/post/create" class="nav-item">发布</router-link>
          <div class="nav-item dropdown">
            关于 <span class="arrow">▼</span>
            <div class="dropdown-menu">
              <router-link v-if="authStore.isLoggedIn" to="/profile" class="dropdown-item">个人主页</router-link>
              <a href="#" class="dropdown-item" v-if="!authStore.isLoggedIn" @click.prevent="router.push('/login')">个人主页</a>
              <a href="#" class="dropdown-item">后台管理</a>
              <a href="#" class="dropdown-item" @click.prevent="handleLogout" v-if="authStore.isLoggedIn">退出账号</a>
              <router-link v-if="!authStore.isLoggedIn" to="/login" class="dropdown-item">登录</router-link>
            </div>
          </div>
        </nav>
      </div>
    </header>

    <!-- 主内容区 -->
    <main class="main-content">
      <!-- 文章内容 -->
      <article class="article-content">
        <div v-if="loading" class="loading-container">
          <p>加载中...</p>
        </div>
        <div v-else-if="!article" class="empty-container">
          <p>文章不存在</p>
        </div>
        <div v-else>
          <h1 class="article-title">{{ article.title }}</h1>
          <div class="article-meta">
            <span class="meta-item">
              <i class="icon-user"></i>
              {{ article.author || 'Steven' }}
            </span>
            <span class="meta-item">
              <i class="icon-date"></i>
              {{ formatDate(article.published_at) }}
            </span>
            <span class="meta-item">
              <i class="icon-view"></i>
              {{ article.view_count }} 次阅读
            </span>
            <span class="meta-item" v-if="article.tags && article.tags.length > 0">
              <i class="icon-tag"></i>
              <span v-for="(tag, index) in article.tags" :key="index">
                {{ tag }}<span v-if="index < article.tags.length - 1">, </span>
              </span>
            </span>
          </div>
          <div class="article-body" v-html="article.content"></div>
        </div>
      </article>

      <!-- 右侧边栏 -->
      <aside class="sidebar">
        <!-- 搜索框 -->
        <div class="search-box">
          <input type="text" placeholder="输入关键词搜索" class="search-input" />
          <button class="search-btn">🔍</button>
        </div>

        <!-- 用户信息卡片 -->
        <div class="user-card">
          <div class="user-avatar">
            <img src="https://py-steven.com/blog/static/image/logo.png" alt="avatar" />
          </div>
          <h3 class="user-name">{{ authStore.user?.nickname || 'liao' }}</h3>
          <p class="user-bio">{{ authStore.user?.email || '热爱编程，热爱生活' }}</p>
          <div class="user-links">
            <a href="#" class="user-link">
              <span class="link-icon">🔗</span>
              Github
            </a>
            <a href="#" class="user-link">
              <span class="link-icon">💻</span>
              Gitee
            </a>
          </div>
          <p class="user-mood">个人信息</p>
        </div>

        <!-- 文章推荐 -->
        <div class="recommend-section">
          <h3 class="section-title">📚 文章推荐</h3>
          <ul class="recommend-list">
            <li class="recommend-item">
              <span class="file-icon">📄</span>
              <a href="#">Python Flask利用sqlalche...</a>
            </li>
            <li class="recommend-item">
              <span class="file-icon">📄</span>
              <a href="#">Awesome Editor!</a>
            </li>
          </ul>
        </div>
      </aside>
    </main>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useAuthStore } from '@/store/auth'
import { logout } from '@/api/auth'
import { getArticleDetail } from '@/api/article'
import message from '@/utils/message'

// 引入 highlight.js 样式（GitHub 主题）
import 'highlight.js/styles/github.css'

const router = useRouter()
const route = useRoute()
const authStore = useAuthStore()

const article = ref(null)
const loading = ref(false)

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  })
}

// 获取文章详情
const fetchArticleDetail = async () => {
  const articleId = route.params.id
  if (!articleId) {
    message.error('文章ID不存在')
    return
  }

  try {
    loading.value = true
    const response = await getArticleDetail(articleId)

    if (response.code === 200) {
      article.value = response.data
    }
  } catch (error) {
    console.error('获取文章详情失败:', error)
    message.error('获取文章详情失败')
  } finally {
    loading.value = false
  }
}

// 退出登录
const handleLogout = async () => {
  try {
    await logout()
    authStore.logout()
    message.success('退出登录成功')
    router.push('/login')
  } catch (error) {
    message.error(error.message || '退出登录失败')
  }
}

// 组件挂载时获取文章详情
onMounted(() => {
  fetchArticleDetail()
})
</script>

<style scoped>
.article-detail-container {
  min-height: 100vh;
  background-color: #f5f7fa;
}

/* 顶部导航栏 */
.header {
  background: white;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.site-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  max-width: 400px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.site-subtitle {
  font-size: 14px;
  color: #999;
  background: #f0f0f0;
  padding: 2px 8px;
  border-radius: 4px;
}

.nav-menu {
  display: flex;
  gap: 30px;
  align-items: center;
}

.nav-item {
  color: #333;
  text-decoration: none;
  font-size: 14px;
  cursor: pointer;
  transition: color 0.3s;
  position: relative;
}

.nav-item:hover {
  color: #1890ff;
}

.nav-item.active {
  color: #1890ff;
  font-weight: 500;
}

.dropdown {
  position: relative;
}

.arrow {
  font-size: 10px;
  margin-left: 4px;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 8px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 8px 0;
  min-width: 140px;
  display: none;
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.2s, transform 0.2s;
}

.dropdown::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  height: 12px;
}

.dropdown:hover .dropdown-menu {
  display: block;
  opacity: 1;
  transform: translateY(0);
}

.dropdown-item {
  display: block;
  padding: 10px 20px;
  color: #333;
  text-decoration: none;
  font-size: 14px;
  transition: background 0.2s;
}

.dropdown-item:hover {
  background: #f5f5f5;
}

/* 主内容区 */
.main-content {
  max-width: 1400px;
  margin: 0 auto;
  padding: 30px 20px;
  display: flex;
  gap: 30px;
}

/* 文章内容 */
.article-content {
  flex: 1;
  background: white;
  border-radius: 8px;
  padding: 40px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  min-width: 0;
}

.loading-container,
.empty-container {
  padding: 60px 20px;
  text-align: center;
}

.loading-container p,
.empty-container p {
  color: #999;
  font-size: 14px;
}

.article-title {
  font-size: 32px;
  color: #333;
  margin: 0 0 20px 0;
  font-weight: 600;
  text-align: center;
}

.article-meta {
  display: flex;
  justify-content: center;
  gap: 20px;
  padding: 20px 0;
  border-bottom: 1px solid #e8e8e8;
  margin-bottom: 30px;
  flex-wrap: wrap;
}

.meta-item {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  color: #999;
}

.icon-user::before { content: '👤'; }
.icon-tag::before { content: '🏷️'; }
.icon-date::before { content: '📅'; }
.icon-view::before { content: '👁️'; }

.article-body {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
  word-wrap: break-word;
}

/* 标题样式 */
.article-body :deep(h1),
.article-body :deep(h2),
.article-body :deep(h3),
.article-body :deep(h4),
.article-body :deep(h5),
.article-body :deep(h6) {
  margin: 24px 0 16px 0;
  font-weight: 600;
  line-height: 1.4;
  color: #333;
}

.article-body :deep(h1) {
  font-size: 28px;
  padding-bottom: 8px;
  border-bottom: 2px solid #eaecef;
}

.article-body :deep(h2) {
  font-size: 24px;
  padding-bottom: 6px;
  border-bottom: 1px solid #eaecef;
}

.article-body :deep(h3) {
  font-size: 20px;
}

.article-body :deep(h4) {
  font-size: 18px;
}

.article-body :deep(h5) {
  font-size: 16px;
}

.article-body :deep(h6) {
  font-size: 14px;
  color: #666;
}

/* 段落样式 */
.article-body :deep(p) {
  margin: 16px 0;
  line-height: 1.8;
  color: #555;
}

/* 代码样式 */
.article-body :deep(code) {
  background: #f6f8fa;
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 13px;
  font-family: 'Courier New', 'Monaco', monospace;
  color: #d73a49;
}

.article-body :deep(pre) {
  background: #f6f8fa;
  padding: 16px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 16px 0;
  line-height: 1.6;
}

.article-body :deep(pre code) {
  background: none;
  padding: 0;
  font-size: 13px;
  line-height: 1.6;
}

/* highlight.js 代码高亮支持 */
.article-body :deep(pre code.hljs) {
  display: block;
  padding: 0;
}

/* 文本样式 */
.article-body :deep(strong) {
  font-weight: 600;
  color: #333;
}

.article-body :deep(em) {
  font-style: italic;
}

.article-body :deep(del) {
  text-decoration: line-through;
  color: #888;
}

/* 链接样式 */
.article-body :deep(a) {
  color: #1890ff;
  text-decoration: none;
}

.article-body :deep(a:hover) {
  text-decoration: underline;
}

/* 图片样式 */
.article-body :deep(img) {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 20px auto;
  border-radius: 4px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* 引用样式 */
.article-body :deep(blockquote) {
  border-left: 4px solid #dfe2e5;
  padding: 8px 16px;
  margin: 16px 0;
  background: #f6f8fa;
  color: #666;
}

/* 列表样式 */
.article-body :deep(ul),
.article-body :deep(ol) {
  margin: 16px 0;
  padding-left: 32px;
}

.article-body :deep(li) {
  margin: 8px 0;
  line-height: 1.8;
  color: #555;
}

.article-body :deep(ul) {
  list-style-type: disc;
}

.article-body :deep(ol) {
  list-style-type: decimal;
}

/* 任务列表样式 */
.article-body :deep(li.task-list) {
  list-style-type: none;
  margin-left: -20px;
}

.article-body :deep(li.task-list.checked) {
  color: #888;
  text-decoration: line-through;
}

/* 分割线样式 */
.article-body :deep(hr) {
  border: none;
  border-top: 2px solid #eaecef;
  margin: 24px 0;
}

/* 表格样式 */
.article-body :deep(table) {
  border-collapse: collapse;
  width: 100%;
  margin: 16px 0;
  border: 1px solid #dfe2e5;
  display: block;
  overflow-x: auto;
}

.article-body :deep(table th),
.article-body :deep(table td) {
  border: 1px solid #dfe2e5;
  padding: 8px 12px;
  text-align: left;
}

.article-body :deep(table th) {
  background: #f6f8fa;
  font-weight: 600;
  color: #333;
}

.article-body :deep(table td) {
  color: #555;
}

/* 右侧边栏 */
.sidebar {
  width: 320px;
  flex-shrink: 0;
}

.search-box {
  background: white;
  border-radius: 8px;
  padding: 16px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  display: flex;
  gap: 8px;
}

.search-input {
  flex: 1;
  border: 1px solid #e8e8e8;
  border-radius: 4px;
  padding: 8px 12px;
  font-size: 14px;
  outline: none;
}

.search-input:focus {
  border-color: #1890ff;
}

.search-btn {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 4px;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 16px;
}

.user-card {
  background: white;
  border-radius: 8px;
  padding: 24px;
  text-align: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  margin-bottom: 20px;
}

.user-avatar {
  width: 100px;
  height: 100px;
  margin: 0 auto 16px;
  border-radius: 50%;
  overflow: hidden;
  border: 4px solid #f0f0f0;
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-name {
  font-size: 18px;
  color: #333;
  margin: 12px 0 8px;
}

.user-bio {
  color: #999;
  font-size: 13px;
  margin: 8px 0;
}

.user-links {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 16px 0;
}

.user-link {
  display: flex;
  align-items: center;
  gap: 4px;
  color: #666;
  text-decoration: none;
  font-size: 13px;
  transition: color 0.3s;
}

.user-link:hover {
  color: #1890ff;
}

.link-icon {
  font-size: 16px;
}

.user-mood {
  color: #ff4d4f;
  font-size: 13px;
  margin-top: 12px;
}

.recommend-section {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.section-title {
  font-size: 15px;
  color: #333;
  margin: 0 0 16px 0;
  font-weight: 600;
}

.recommend-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.recommend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-bottom: 1px solid #f0f0f0;
}

.recommend-item:last-child {
  border-bottom: none;
}

.file-icon {
  font-size: 16px;
}

.recommend-item a {
  color: #666;
  text-decoration: none;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  transition: color 0.3s;
}

.recommend-item a:hover {
  color: #1890ff;
}

/* 响应式设计 */
@media (max-width: 1024px) {
  .main-content {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
  }

  .article-content {
    padding: 24px;
  }

  .article-title {
    font-size: 24px;
  }
}
</style>
